import { defineStore } from 'pinia';

export const useWeatherStore = defineStore('weather', {
  state: () => ({
    weatherList: [],
  }),
  actions: {
    addWeatherData(data) {
      console.log('添加的数据:', data); // 这行代码将帮助你确认所添加的数据
      this.weatherList.push(data);
      this.saveData('weatherList', this.weatherList);
    },
    // 保存数据到 localStorage
    saveData(key, data) {
      localStorage.setItem(key, JSON.stringify(data));
    },
    // 从 localStorage 获取数据
    loadData(key) {
      const data = localStorage.getItem(key);
      if (data) {
        this.weatherList = JSON.parse(data);
        console.log('加载的数据:', this.weatherList);
      } else {
        console.log('没有找到持久化的数据');
      }
    },
    // 删除 localStorage 中的数据
    removeWeatherData(index) {
      this.weatherList.splice(index, 1); // 根据索引删除元素
      this.saveData('weatherList', this.weatherList);
    },
  },
});